lapop12 = readRDS('data/lapop12.rds')

#### Falsification #### 

#### coding outcomes #### 
lapop12$b31 <- as.character(lapop12$b31)
lapop12$b31[lapop12$b31=="Nada de confianza"] <- 1 
lapop12$b31[lapop12$b31=="Mucha confianza"] <- 7 
lapop12$trust_sc <- as.numeric(lapop12$b31)

lapop12$b13 <- as.character(lapop12$b13)
lapop12$b13[lapop12$b13=="Nada de confianza"] <- 1 
lapop12$b13[lapop12$b13=="Mucha confianza"] <- 7 
lapop12$trust_leg <- as.numeric(lapop12$b13)

lapop12$b32 <- as.character(lapop12$b32)
lapop12$b32[lapop12$b32=="Nada de confianza"] <- 1 
lapop12$b32[lapop12$b32=="Mucha confianza"] <- 7 
lapop12$trust_local <- as.numeric(lapop12$b32)

lapop12$b21a <- as.character(lapop12$b21a)
lapop12$b21a[lapop12$b21a=="Nada de confianza"] <- 1 
lapop12$b21a[lapop12$b21a=="Mucha confianza"] <- 7 
lapop12$trust_pres <- as.numeric(lapop12$b21a)

lapop12$it1 <- as.character(lapop12$it1)
lapop12$it1[lapop12$it1=="Nada confiable"] <- 1 
lapop12$it1[lapop12$it1=="Poco confiable"] <- 2
lapop12$it1[lapop12$it1=="Algo confiable"] <- 3 
lapop12$it1[lapop12$it1=="Muy confiable"] <- 4 
lapop12$social_trust <- as.numeric(lapop12$it1)

lapop12$b18 <- as.character(lapop12$b18)
lapop12$b18[lapop12$b18=="Nada de confianza"] <- 1 
lapop12$b18[lapop12$b18=="Mucha confianza"] <- 7 
lapop12$trust_poli <- as.numeric(lapop12$b18)



m1 <- lm_robust(trust_sc ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')
m2 <- lm_robust(trust_leg ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')
m3 <- lm_robust(trust_local ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')
m4 <- lm_robust(trust_pres ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')
m6 <- lm_robust(social_trust ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')
m5 <- lm_robust(trust_poli ~ trial*trend, data = subset(lapop12, trend < 8 & trend > -8),
                fixed_effects = ur + estratopri + estratosec, 
                se_type = 'stata')


coef.name <- c('Supreme Court', 'Legislature', 'Local Gov.',  'President',  'Police', 'Social')
y <- c(summary(m1)$coefficients[1,1], summary(m2)$coefficients[1,1], summary(m3)$coefficients[1,1], 
       summary(m4)$coefficients[1,1], summary(m5)$coefficients[1,1], summary(m6)$coefficients[1,1])  
U <- c(summary(m1)$coefficients[1,6], summary(m2)$coefficients[1,6], summary(m3)$coefficients[1,6], 
       summary(m4)$coefficients[1,6], summary(m5)$coefficients[1,6], summary(m6)$coefficients[1,6])  
L <- c(summary(m1)$coefficients[1,5], summary(m2)$coefficients[1,5], summary(m3)$coefficients[1,5], 
       summary(m4)$coefficients[1,5], summary(m5)$coefficients[1,5], summary(m6)$coefficients[1,5])  
pv <- c(m1$p.value[1], m2$p.value[1],  
        m3$p.value[1], m4$p.value[1],
        m5$p.value[1], m6$p.value[1])

plot.df <- data.frame(coef.name, y, U, L, pv)
plot.df$y2 <- round(plot.df$y, 2)
plot.df$y2 <- ifelse(plot.df$pv < .05 & plot.df$pv > .01, paste0(plot.df$y2, "*"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .01 & plot.df$pv > .001, paste0(plot.df$y2, "**"), plot.df$y2)
plot.df$y2 <- ifelse(plot.df$pv < .001, paste0(plot.df$y2, "***"), plot.df$y2)

falsification <- ggplot(plot.df, aes(x = coef.name, y = y)) +
  geom_point(size = 1) +
  geom_errorbar(aes(ymax = U, ymin = L), width=.1,
                position=position_dodge(.9)) + 
  geom_hline(yintercept = 0) + 
  ggtitle("Falsification Tests: Trust") + 
  xlab("Outcome") +
  ylab("Partial Derivative") +
  annotate("label", x = coef.name, y = y, label = plot.df$y2, 
           hjust=.5, vjust=1, family="Times") +
  theme_tufte()

falsification

ggsave("fig-out/falsification.pdf", width = 10)

